package flex.cc.store.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import flex.cc.PublicUtil;
import flex.cc.store.dto.entity.StoreInfoEntity;
import flex.cc.store.service.StoreInfoService;
import flex.cc.store.service.StorePushService;

/**
 * 
 * @author WANGCONG
 * 2021年12月20日下午13:19:46
 * @desc 网点推送对接接口业务实现层
 */
@Service("storePushService")
@Transactional(rollbackFor = Exception.class)
public class StorePushServiceImpl implements StorePushService{

	@Resource
	private StoreInfoService storeInfoService;
	
	/**
	 * 
	 * @author WANGCONG
	 * 2021年12月20日下午13:20:51
	 * @desc 网点信息解析保存
	 */
	@Override
	public void saveShop(String shopInfo) {
		//解析JSON报文，进行数据新增、更新、删除
		JSONObject jsonObject = JSONObject.parseObject(shopInfo);
		String status = (String) jsonObject.get("status"); //网点与机构状态：新增(ADD),修改(UPDATE),删除(DELETE)
		String shopCode = (String) jsonObject.get("shopCode"); //网点编号，网点唯一标识
		String apiCode = (String) jsonObject.get("apiCode"); //服务商唯一识别码，目前盛大没有这个字段，后期介入的新服务商增加这个字段，没值默认为盛大的
		apiCode = PublicUtil.isNotEmpty(apiCode) ? apiCode.trim() : "SD";
		if("DELETE".equals(status)){
			//根据shopCode删除对应的门点信息（逻辑删除）
			StoreInfoEntity delVo = new StoreInfoEntity();
			delVo.setIsDelete(1);
			delVo.setShopCode(shopCode);
			delVo.setStatus(status);
			delVo.setUpdateTime(new Date());
			delVo.setApiCode(apiCode);
			storeInfoService.delete(delVo);
		}else{
			String shopName = (String) jsonObject.get("shopName"); //网点名称
			String proName = (String) jsonObject.get("proName");   //网点省份
			String cityName = (String) jsonObject.get("cityName"); //网点城市
			String areaName = (String) jsonObject.get("areaName"); //网点县区
			String address = (String) jsonObject.get("address");   //网点详细地址
			String coordinate = (String) jsonObject.get("coordinate"); //网点百度坐标
			String gcjCoordinate = (String) jsonObject.get("gcjCoordinate"); //网点高德坐标
			String contactName = (String) jsonObject.get("contactName"); //网点联系人
			String telephone = (String) jsonObject.get("telephone");   //网点联系电话
			String doorPhotoUrl = (String) jsonObject.get("doorPhotoUrl"); //网点门头照
			String logoImgPath = (String) jsonObject.get("logoImgPath"); //网点logo大图
			String openTime = (String) jsonObject.get("openTime"); //营业开始时间
			String restTime = (String) jsonObject.get("restTime");   //营业结束时间
			String isStatus = (String) jsonObject.get("isStatus"); //网点状态,2:停用（解约）,3:下线（下线）,4:有效（新增，上线）
			String sourceCode = (String) jsonObject.get("sourceCode"); //网点关联机构信息
			String rating = (String) jsonObject.get("rating");   //网点评级
			String shopOrderNum = (String) jsonObject.get("shopOrderNum"); //商户订单总数量
			String areaNumber = (String) jsonObject.get("areaNumber"); //区域编码
			String proNumber = (String) jsonObject.get("proNumber"); //省编码
			String cityNumber = (String) jsonObject.get("cityNumber");   //市编码
			String serviceIds = ""; //服务小项集合
			StringBuffer sb = new StringBuffer();
			JSONArray dList = jsonObject.getJSONArray("serviceDetails");//服务大项集合
			if(dList!=null){
				for(int i=0; i<dList.size(); i++){
					JSONObject dObj = dList.getJSONObject(i);
					JSONArray xList = dObj.getJSONArray("serviceList");//服务小项集合
					if(xList!=null){
						for(int j=0; j<xList.size(); j++){
							JSONObject xObj = xList.getJSONObject(j);
							Integer serviceId = (Integer) xObj.get("serviceId");//服务小项主键ID
							sb.append(serviceId).append(",");
						}
					}
				}
				if(sb.length()>0){
					serviceIds = sb.substring(0, sb.length()-1);
				}
			}
			StoreInfoEntity storeInfoEntity = new StoreInfoEntity(shopName, shopCode, proName, cityName, 
					areaName, address, coordinate, gcjCoordinate, contactName, telephone, doorPhotoUrl, logoImgPath, 
					openTime, restTime, isStatus, sourceCode, status, rating, Integer.valueOf(shopOrderNum), areaNumber, proNumber, 
					cityNumber, serviceIds);
			Map<String, Object> storeInfoParams = new HashMap<>();
			storeInfoParams.put("shopCode", shopCode);
			storeInfoParams.put("apiCode", apiCode);
			StoreInfoEntity stroeVo = storeInfoService.getOneByParams(storeInfoParams);
			if(stroeVo==null){//不管是新增还是更新，数据库中没有的，直接按新增处理
				storeInfoEntity.setIsDelete(0);
				storeInfoEntity.setCreater("admin");
				storeInfoEntity.setUpdater("admin");
				storeInfoEntity.setCreateTime(new Date());
				storeInfoEntity.setUpdateTime(new Date());
				storeInfoEntity.setApiCode(apiCode);
				storeInfoService.insert(storeInfoEntity);
			}else{
				if("ADD".equals(status)){
					Integer isDelete = stroeVo.getIsDelete();
					if(isDelete==1){//已删除，再次重新关联，更新处理
						storeInfoEntity.setIsDelete(0);
						storeInfoEntity.setUpdater("admin");
						storeInfoEntity.setUpdateTime(new Date());
						storeInfoEntity.setApiCode(apiCode);
						storeInfoService.update(storeInfoEntity);
					}else{
						//已存在，不做保存
					}
				}else{
					storeInfoEntity.setUpdateTime(new Date());
					storeInfoEntity.setStatus(status);
					storeInfoEntity.setApiCode(apiCode);
					storeInfoService.update(storeInfoEntity);
				}
			}
		}
	}

}
